package com.pro_one.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pro_one.dao.CheckGroupDao;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.pojo.CheckGroup;
import com.pro_one.pojo.CheckItem;
import com.pro_one.service.CheckGroupService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
@Transactional(readOnly = true)
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    /**
     * 展示列表以及根据条件查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findByCondition(QueryPageBean queryPageBean) {
//        System.out.println("aaaaaaa");
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        List<CheckGroup> checkGroups = checkGroupDao.findByCondition(queryString);
        PageInfo pageInfo = new PageInfo(checkGroups);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 添加检查组功能
     *
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.addCheckGroup(checkGroup);
//        System.out.println(checkitemIds);
        this.addCheckitemIdByCheckGroupId(checkGroup, checkitemIds);
    }

    /**
     * 查询单个检查组
     *
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);
        return checkGroup;
    }

    /**
     * 根据检查组id查询所有关联的检查项id
     *
     * @param id
     * @return
     */
    @Override
    public List<Integer> findCheckitemIdsByCheckgroupId(Integer id) {
        List<Integer> checkItemIds = checkGroupDao.findCheckitemIdsByCheckgroupId(id);
        return checkItemIds;
    }

    /**
     * 修改检查组以及关联的检查项信息
     *
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.edit(checkGroup);
        checkGroupDao.deleteCheckitemIdsByGroupId(checkGroup.getId());
        this.addCheckitemIdByCheckGroupId(checkGroup, checkitemIds);
    }

    /**
     * 根据检查组id查询已经关联的检查项id，并实现分页以及倒序排列
     *
     * @param queryPageBean
     * @param id
     * @return
     */
    @Override
    public PageResult findCheckedCheckItemByGroupId(QueryPageBean queryPageBean, Integer id) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        List<CheckItem> checkItemIds = checkGroupDao.findCheckedCheckItemByGroupId(id);
        System.out.println(checkItemIds);
        PageInfo pageInfo = new PageInfo(checkItemIds);
        System.out.println(pageInfo);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 删除检查组功能
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(Integer id) {
        List<Integer> checkitemIds = checkGroupDao.findCheckitemIdsByCheckgroupId(id);
        if (checkitemIds != null && checkitemIds.size() > 0) {
            checkGroupDao.deleteCheckitemIdsByGroupId(id);
        }
        checkGroupDao.deleteCheckGroupById(id);
    }

    /**
     * 根据检查组id添加关联检查项
     *
     * @param checkGroup
     * @param checkitemIds
     */
    public void addCheckitemIdByCheckGroupId(CheckGroup checkGroup, Integer[] checkitemIds) {
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                checkGroupDao.addCheckitemIdByCheckGroupId(checkGroup.getId(), checkitemId);
            }
        }
    }
}
